package com.handinfo.util;


import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScene;
import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.Actor;
import com.handinfo.engine.GameManager;

public class UIUtil
{
	public static int waitTime = 2000; // 按键等待时间，超过则视为按下按键

	public static final byte FRAME_SIZE = 6; // 边框大小
	public static final byte BLANK_SIZE = 5; // 间隔大小
	public static final byte DLG_HEIGHT = 53;

	public static int dialogColor1 = 0x000000; // 字体颜色
	public static int dialogColor2 = 0xFFFFFF; // 字体阴影颜色

	public static int lastW = 0; // 临时变量
	public static int lastH = 0; // 临时变量

	/**
	 * 显示剧情对话框
	 * 
	 * @param g
	 * @param name
	 * 名字
	 * @param dialog
	 * 正文内容
	 * @param frame
	 * 对话显示完成后停留帧数
	 * @param faceId
	 * 头像ID
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param left
	 * 是否头像在左边
	 */
	public static void showDialog(Graphics g, String name, String dialog,
			int frame, Actor actor, int x, int y, int w, int h, boolean left)
	{
		if (Tools.FAST) {
			return;
		}
		if (dialog == null || dialog.length() == 0) {
			return;
		}
		if (h < g.getFont().getHeight()) {
			if (Tools.DEBUG) {
				System.out.println("Error: not enough HEIGHT to draw.");
			}
			return;
		}
		if (w < g.getFont().charWidth(dialog.charAt(0))) {
			if (Tools.DEBUG) {
				System.out.println("Error: not enough WIDTH to draw.");
			}
			return;
		}
		AbstractGameScreen gc = GameManager.getInstance().m_screen;
		waitTime = frame * GameManager.FRAME_INTERVAL;
		drawFrame(g, x, y, w, h, true);
		if (name != null && name.length() > 0) {
			final int dw = 86;
			drawFrame(g, x, y - 1 - 21, dw, 22, false);
			g.setColor(0);
			AbstractGameScreen.drawString(g, name, x + dw / 2, y - 8, Graphics.VCENTER | Graphics.HCENTER);
		}
		// 播放表情
		if (null != actor) {
			actor.m_anim.draw(gc.getGraphics(),
					AbstractGameScreen.SCREEN_WIDTH - 40,
					AbstractGameScreen.SCREEN_HEIGHT - DLG_HEIGHT, false,
					false, actor.m_frameIndex);
			actor.nextFrame();
		}

		lastW = w - FRAME_SIZE * 2;
		lastH = h - FRAME_SIZE * 2;
		int fontW = 0;
		int fontH = g.getFont().getHeight();
		int startX = x + FRAME_SIZE + 2, startY = y + FRAME_SIZE + 2;
		int checki = 0;
		boolean skip = true; // 不用逐字显示
		// 逐字显示文字内容
		for (int i = 0; i < dialog.length(); i++) {
			AbstractGameScreen.updateKeyBegin();
			if (AbstractGameScreen.keyConfirmed()) {
				skip = true;
			}
			fontW = g.getFont().charWidth(dialog.charAt(i));
			checki = check(dialog, 0, fontW, fontH, i);
			if (checki != 0) {
				i += (checki - 1);
				continue;
			}
			if (lastW < fontW) { // 没有空格，需要回行
				lastH -= (fontH + 2);
				lastW = w - FRAME_SIZE * 2;
				startX = x + FRAME_SIZE + 2;
				startY += (fontH + 2);
			}
			if (lastH <= fontH) { // 没有空行了，需要翻页
				skip = true;
				gc.flush();
				GameManager.waitAnyKey(waitTime, AbstractGameScreen.GK_A
						| AbstractGameScreen.GK_LEFT_SOFT
						| AbstractGameScreen.GK_RIGHT_SOFT);
				// drawBox(g, x, y, w, h, true, BG_DIALOG);
				drawFrame(g, x, y, w, h, true);
				// drawBackground(g, x, y, w, h, BG_LIGHT, BOX_BORDER_COLOR,
				// true);
				lastH = h - FRAME_SIZE * 2;
				lastW = w - FRAME_SIZE * 2;
				startX = x + FRAME_SIZE + 2;
				startY = y + FRAME_SIZE + 2;
			}
			if (!skip) {
				AbstractGameScreen.setClip(g, startX, startY, fontW + 2,
						fontH + 2);
				// g.setColor(dialogColor2);
				// g.drawChar(dialog.charAt(i), startX + 1, startY + 1, 0);
				g.setColor(dialogColor1);
				g.drawChar(dialog.charAt(i), startX, startY, 0);
				AbstractGameScreen.restoreClip(g);
				gc.flush();
				GameManager.waitAnyTime(GameManager.FRAME_INTERVAL);
			}
			else {
				// g.setColor(dialogColor2);
				// g.drawChar(dialog.charAt(i), startX + 1, startY + 1, 0);
				g.setColor(dialogColor1);
				g.drawChar(dialog.charAt(i), startX, startY, 0);
			}
			startX += (fontW + 2);
			lastW -= (fontW + 2);
		}
		gc.flush();
		GameManager.waitAnyKey(waitTime, AbstractGameScreen.GK_A
				| AbstractGameScreen.GK_LEFT_SOFT
				| AbstractGameScreen.GK_RIGHT_SOFT);
	}
	
    /**
     * 显示诗句
     * @param g
     * @param dialog 正文内容
     * @param frame 诗句显示完成后停留帧数
     * @param direction 方向
     * @param x
     * @param y
     * @param w
     * @param h
     */
    public static void showPoem(AbstractGameScene scene, Graphics g, String dialog, int frame, int direction, int x, int y, int w,
                                int h, int fontW, int fontH)
    {
        if (Tools.FAST) {
            return;
        }
        if (dialog == null) {
            return;
        }
        if (h < g.getFont().getHeight()) {
            System.out.println("Error: not enough HEIGHT to draw.");
            return;
        }
        if (w < g.getFont().charWidth(dialog.charAt(0))) {
            System.out.println("Error: not enough WIDTH to draw.");
            return;
        }
        AbstractGameScreen gc = GameManager.getInstance().getScreen();
        lastW = w;
        lastH = h;
        int startX = x, startY = y;
        if (direction == 1) {
            startX = x + w - fontW;
        }
        int checki = 0;
        boolean skip = false;
        // 逐字显示文字内容
        for (int i = 0; i < dialog.length(); i++) {
            // 检查特殊字符串，并跳过特殊字符串所占的位置
            checki = check(dialog, direction, fontW, fontH, i);
            //用于显示闪烁的 "_"
            if (checki != 0) {
                i += (checki - 1);
                continue;
            }
            if (checki != 2 && checki != 3){
                if (direction == 0) { // 如果是横向写诗句
                    if (lastW < fontW) { // 没有空格，需要回行
                        lastH -= fontH;
                        lastW = w;
                        startX = x;
                        startY += fontH;
                    }
                    if (lastH < fontH) { // 没有空行了，需要翻页
                        skip = false;
                        gc.flush();
                        GameManager.waitAnyKey();
                        scene.draw(g);
                        lastH = h;
                        lastW = w;
                        startX = x;
                        startY = y;
                    }
                }
                else { // 如果是纵向写诗句
                    if (lastH < fontH) { // 没有空格，需要回行
                        lastW -= fontW;
                        lastH = h;
                        startY = y;
                        startX -= fontW;
                    }
                    if (lastW < fontW) { // 没有空行了，需要翻页
                        skip = false;
                        gc.flush();
                        GameManager.waitAnyKey();
                        scene.draw(g);
                        lastH = h;
                        lastW = w;
                        startX = x + w - fontW;
                        startY = y;
                    }
                }
            }
            AbstractGameScreen.updateKeyBegin();
            if (AbstractGameScreen.keyConfirmed()) {
                skip = true;
            }
            AbstractGameScreen.updateKeyEnd();
            int dialogColor1 = 0xFFD400;
            int dialogColor2 = 0x943305;
            if (!skip) {
                AbstractGameScreen.setClip(g, startX - 1, startY - 1, fontW, fontH + 2);
                Tools.drawString(g, startX, startY, dialog.substring(i, i + 1), dialogColor1, dialogColor2, 0);
                AbstractGameScreen.restoreClip(g);
                gc.flush();
//                for (int k = 3; k > 0; k--) {
//                    AbstractGameScreen.setClip(g, startX - 1, startY - 1, (fontW + 2) / k, (fontH + 2) / k);
//                    Tools.drawShineString(g, dialog.substring(i, i + 1), startX, startY, dialogColor2, dialogColor1,
//                                          0);
//                    AbstractGameScreen.restoreClip(g);
//                    gc.flush();
//                }
                GameManager.waitAnyKey(100);
            }
            else {
                Tools.drawString(g, startX, startY, dialog.substring(i, i + 1), dialogColor1, dialogColor2, 0);
            }
            if (direction == 0) { // 如果是横向写诗句
                startX += fontW;
                lastW -= fontW;
            }
            else { // 如果是纵向写诗句
                startY += fontH;
                lastH -= fontH;
            }
        }
//        UI.drawSkipCG(g);
        gc.flush();
        GameManager.waitAnyKey();
    }

	public static final int FRAME_CORNER1_SRCX = 37;
	public static final int FRAME_CORNER1_SRCY = 0;
	public static final int FRAME_CORNER1_W = 7;
	public static final int FRAME_CORNER1_H = 21;
	public static final int FRAME_CORNER2_SRCX = 0;
	public static final int FRAME_CORNER2_SRCY = 0;
	public static final int FRAME_CORNER2_W = 36;
	public static final int FRAME_CORNER2_H = 7;

	/**
	 * 绘制一个空的信息框
	 * 
	 * @param g
	 * Graphics
	 * @param x
	 * int
	 * @param y
	 * int
	 * @param w
	 * int
	 * @param h
	 * int
	 * @param hasBorder
	 * boolean 边角
	 * @param bgtype
	 * int 背景图
	 */
	public static void drawFrame(Graphics g, int x, int y, int w, int h,
			boolean hasCorner)
	{
		// AbstractGameScreen.drawRegion(g, s_img_dlg_panel, FRAME_CORNER1_SRCX,
		// FRAME_CORNER1_SRCY, FRAME_CORNER1_W, FRAME_CORNER1_H, 0, x, y, 0);
		// AbstractGameScreen.drawRegion(g, s_img_dlg_panel, FRAME_CORNER1_SRCX,
		// FRAME_CORNER1_SRCY, FRAME_CORNER1_W, FRAME_CORNER1_H,
		// AbstractGameScreen.trans(1), x + w, y, g.TOP|g.RIGHT);
		// AbstractGameScreen.drawRegion(g, s_img_dlg_panel, FRAME_CORNER2_SRCX,
		// FRAME_CORNER2_SRCY, FRAME_CORNER2_W, FRAME_CORNER2_H, 0, x, y + h,
		// g.BOTTOM|g.LEFT);
		// AbstractGameScreen.drawRegion(g, s_img_dlg_panel, FRAME_CORNER2_SRCX,
		// FRAME_CORNER2_SRCY, FRAME_CORNER2_W, FRAME_CORNER2_H,
		// AbstractGameScreen.trans(1), x + w, y + h, g.BOTTOM|g.RIGHT);
		g.setClip(x, y, w, h);
		g.setColor(0x11111d);
		g.drawLine(x + FRAME_CORNER1_W, y, x + w - FRAME_CORNER1_W, y);
		g.setColor(0xeafcf9);
		g.drawLine(x + FRAME_CORNER1_W, y + 1, x + w - FRAME_CORNER1_W, y + 1);
		g.setColor(0x99c2bd);
		g.drawLine(x + FRAME_CORNER1_W, y + 2, x + w - FRAME_CORNER1_W, y + 2);
		g.setColor(0x587072);
		g.drawLine(x + FRAME_CORNER1_W, y + 3, x + w - FRAME_CORNER1_W, y + 3);
		g.setColor(0x11111d);
		g.drawLine(x + FRAME_CORNER1_W, y + 4, x + w - FRAME_CORNER1_W, y + 4);

		g.setColor(0x11111d);
		g.drawLine(x + FRAME_CORNER2_W, y + h - FRAME_CORNER2_H, x + w
				- FRAME_CORNER2_W, y + h - FRAME_CORNER2_H);
		g.setColor(0xeafcf9);
		g.drawLine(x + FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 1, x + w
				- FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 1);
		g.setColor(0x99c2bd);
		g.drawLine(x + FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 2, x + w
				- FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 2);
		g.setColor(0x587072);
		g.drawLine(x + FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 3, x + w
				- FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 3);
		g.setColor(0x11111d);
		g.drawLine(x + FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 4, x + w
				- FRAME_CORNER2_W, y + h - FRAME_CORNER2_H + 4);

		g.setColor(0x11111d);
		g.drawLine(x + FRAME_CORNER1_W - 1, y + FRAME_CORNER1_H, x
				+ FRAME_CORNER1_W - 1, y + h - FRAME_CORNER2_H);
		g.setColor(0x99c2bd);
		g.drawLine(x + FRAME_CORNER1_W - 2, y + FRAME_CORNER1_H, x
				+ FRAME_CORNER1_W - 2, y + h - FRAME_CORNER2_H);
		g.setColor(0x050505);
		g.drawLine(x + FRAME_CORNER1_W - 3, y + FRAME_CORNER1_H, x
				+ FRAME_CORNER1_W - 3, y + h - FRAME_CORNER2_H);
		g.setColor(0x587072);
		g.drawLine(x + FRAME_CORNER1_W - 4, y + FRAME_CORNER1_H, x
				+ FRAME_CORNER1_W - 4, y + h - FRAME_CORNER2_H);
		g.drawLine(x + FRAME_CORNER1_W - 5, y + FRAME_CORNER1_H, x
				+ FRAME_CORNER1_W - 5, y + h - FRAME_CORNER2_H);
		g.setColor(0x11111d);
		g.drawLine(x + FRAME_CORNER1_W - 6, y + FRAME_CORNER1_H, x
				+ FRAME_CORNER1_W - 6, y + h - FRAME_CORNER2_H);

		g.setColor(0x11111d);
		g.drawLine(x + w - FRAME_CORNER1_W, y + FRAME_CORNER1_H, x + w
				- FRAME_CORNER1_W, y + h - FRAME_CORNER2_H);
		g.setColor(0x99c2bd);
		g.drawLine(x + w - FRAME_CORNER1_W + 1, y + FRAME_CORNER1_H, x + w
				- FRAME_CORNER1_W + 1, y + h - FRAME_CORNER2_H);
		g.setColor(0x050505);
		g.drawLine(x + w - FRAME_CORNER1_W + 2, y + FRAME_CORNER1_H, x + w
				- FRAME_CORNER1_W + 2, y + h - FRAME_CORNER2_H);
		g.setColor(0x587072);
		g.drawLine(x + w - FRAME_CORNER1_W + 3, y + FRAME_CORNER1_H, x + w
				- FRAME_CORNER1_W + 3, y + h - FRAME_CORNER2_H);
		g.drawLine(x + w - FRAME_CORNER1_W + 4, y + FRAME_CORNER1_H, x + w
				- FRAME_CORNER1_W + 4, y + h - FRAME_CORNER2_H);
		g.setColor(0x11111d);
		g.drawLine(x + w - FRAME_CORNER1_W + 5, y + FRAME_CORNER1_H, x + w
				- FRAME_CORNER1_W + 5, y + h - FRAME_CORNER2_H);

		g.setColor(0xb8d0a6);
		g.fillRect(x + FRAME_CORNER1_W, y + 5, w - FRAME_CORNER1_W * 2, h - 5
				- FRAME_CORNER2_H);

		// drawFillKuang(g, x, y, w, h, 0, 0, 0, hasCorner, hasCorner);
	}

	/**
	 * 检查字符串中的特殊字符
	 * 
	 * @param dialog
	 * @param at
	 * @return
	 */
	private static int check(String dialog, int direction, int fontW,
			int fontH, int at)
	{
		int res = 0;
		char c = dialog.charAt(at);
		if (c == '|') { // 换行
			if (direction == 0) {
				lastW = fontW - 1;
			}
			else {
				lastH = fontH - 1;
			}
			res = 1;
		}
		else if (c == '$') { // 变色
			String tmp = dialog.substring(at + 1, at + 7);
			dialogColor1 = Integer.parseInt(tmp, 16);
			res = 7;
		}
		else if (c == '%') { // 原地下划线
			res = 2;
		}
		else if (c == '^') { // 原地空格
			res = 3;
		}
		return res;
	}
}
